// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.assets.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.assets.controller.request.ImportAssetsExpenseReq;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsExpenseReq;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsExpenseReq;
import com.liuxinlong.modules.assets.service.AssetsExpenseService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.ExpenseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 费用维护管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/assets/expense")
public class AssetsExpenseController {
    @Autowired
    private AssetsExpenseService assetsExpenseService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询费用维护管理信息列表
     *
     * @param req 请求信息
     * @return 费用维护管理信息列表
     */
    @PostMapping("page")
    public BaseResp pageAssetsExpenseList(@RequestBody QueryAssetsExpenseReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("year", req.getYear());
        queryMap.put("department", req.getDepartment());
        queryMap.put("accountNumber", req.getAccountNumber());
        queryMap.put("domain", req.getDomain());
        List<Map<String, Object>> dataList = assetsExpenseService.pageAssetsExpenseInfo(queryMap);
        int totalNum = assetsExpenseService.getAssetsExpenseInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询费用维护信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增费用维护管信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增费用维护管信息", business = LogBusinessConstants.ASSETS_EXPENSE_MANAGEMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsExpenseInfo(@RequestBody UpdateAssetsExpenseReq req) {
        ExpenseOrder expenseOrder = new ExpenseOrder();
        expenseOrder.setAccountBook(req.getAccountBook());
        expenseOrder.setYear(req.getYear());
        expenseOrder.setMonth(req.getMonth());
        expenseOrder.setDepartment(req.getDepartment());
        expenseOrder.setDesc(req.getDesc());
        expenseOrder.setAccountNumber(req.getAccountNumber());
        expenseOrder.setProductType(req.getProductType());
        expenseOrder.setAmount(req.getAmount());
        expenseOrder.setBrief(req.getBrief());
        assetsExpenseService.addAssetsExpenseInfo(expenseOrder);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增费用维护管信息成功");
        return resp;
    }

    /**
     * 修改费用维护信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改费用维护信息", business = LogBusinessConstants.ASSETS_EXPENSE_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsExpenseInfo(@RequestBody UpdateAssetsExpenseReq req) {
        ExpenseOrder expenseOrder = new ExpenseOrder();
        expenseOrder.setId(req.getId());
        expenseOrder.setAccountBook(req.getAccountBook());
        expenseOrder.setYear(req.getYear());
        expenseOrder.setMonth(req.getMonth());
        expenseOrder.setDepartment(req.getDepartment());
        expenseOrder.setDesc(req.getDesc());
        expenseOrder.setAccountNumber(req.getAccountNumber());
        expenseOrder.setProductType(req.getProductType());
        expenseOrder.setAmount(req.getAmount());
        expenseOrder.setBrief(req.getBrief());
        assetsExpenseService.updateAssetsExpenseInfo(expenseOrder);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改费用维护息成功");
        return resp;
    }

    /**
     * 删除费用维护信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除费用维护信息", business = LogBusinessConstants.ASSETS_EXPENSE_MANAGEMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsExpenseInfo(@RequestBody BaseDeleteReq req) {
        assetsExpenseService.deleteAssetsExpenseInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除费用维护信息成功");
        return resp;
    }

    /**
     * 导出费用维护信息
     *
     * @param response response
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出费用维护信息", business = LogBusinessConstants.ASSETS_EXPENSE_MANAGEMENT, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportAssetsInfo(@RequestBody QueryAssetsReq req, HttpServletResponse response) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("currentYear", req.getCurrentYear());
        queryMap.put("department", req.getDepartment());
        queryMap.put("projectManage", req.getProjectManage());
        queryMap.put("projectType", req.getProjectType());
        queryMap.put("status", req.getStatus());
        assetsExpenseService.exportAssetsExpense(queryMap, response);
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入费用维护数据", business = LogBusinessConstants.ASSETS_EXPENSE_MANAGEMENT, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importAssetsInfo(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = assetsExpenseService.importAssetsExpense((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入费用维护信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入费用维护数据", business = LogBusinessConstants.ASSETS_EXPENSE_MANAGEMENT, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddAssetsInfo(@RequestBody ImportAssetsExpenseReq req) {
        return assetsExpenseService.batchAddAssetsExpense(req.getDataList());
    }
}
